/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.actions;

import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ipo.mm.ejb.entities.City;
import org.ipo.mm.ejb.entities.Institution;
import org.ipo.mm.ejb.entities.InstitutionFamily;
import org.ipo.mm.ejb.entities.InstitutionRegion;
import org.ipo.mm.ejb.entities.InstitutionType;
import org.ipo.mm.ejb.entities.Meeting;
import org.ipo.mm.ejb.entities.Representative;
import org.ipo.mm.ejb.facade.city.CityFacadeLocal;
import org.ipo.mm.ejb.facade.family.InstitutionFamilyFacadeLocal;
import org.ipo.mm.ejb.facade.institution.InstitutionFacadeLocal;
import org.ipo.mm.ejb.facade.meeting.MeetingFacadeLocal;
import org.ipo.mm.ejb.facade.region.InstitutionRegionFacadeLocal;
import org.ipo.mm.ejb.facade.representative.RepresentativeFacadeLocal;
import org.ipo.mm.ejb.facade.type.InstitutionTypeFacadeLocal;
import org.ipo.mm.web.formbeans.QuickSearchBean;
import org.ipo.mm.web.util.ConstantManager;

/**
 *
 * @author makcro
 */
public class QuickSearchAction extends org.apache.struts.action.Action {

    private String defaultOperator = "OR";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ActionForward actionForward = null;
        injectAdvSearchBaseData(request);
        QuickSearchBean qsb = (QuickSearchBean) form;
        String query = qsb.getQuery().trim();

        boolean doMeetings = qsb.isMeetings();
        boolean doRepresentatives = qsb.isRepresentatives();
        boolean doInstitutions = qsb.isInstitutions();

        // if all selected or none selected do search through all entitites
        if(
                (doMeetings && doRepresentatives && doInstitutions)
                ||
                (!doMeetings && !doRepresentatives && !doInstitutions)
                ){
            
            doSearchMeetings(query, request);
            doSearchRepresentatives(query, request);
            doSearchInstitutions(query, request);
        }
        else {

            if(doMeetings){
                doSearchMeetings(query, request);
            }
            if(doRepresentatives){
                doSearchRepresentatives(query, request);
            }

            if(doInstitutions){
                doSearchInstitutions(query, request);
            }


        }


        actionForward = mapping.findForward(ConstantManager.NAVIGATION_QUICKSEARCH);

        return actionForward;
    }

    private void doSearchMeetings(String query, HttpServletRequest request) {
        Collection<Meeting> meetings = lookupMeetingFacade().search(query, defaultOperator, query, defaultOperator, query, defaultOperator, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "date", -1, -1, true);
        request.setAttribute("MEETING_SEARCH_RESULTS", meetings);
    }

    private void doSearchRepresentatives(String query, HttpServletRequest request) {
        Collection<Representative> representatives = lookupRepresentativeFacade().search(query, defaultOperator, query, defaultOperator, query, defaultOperator, query, defaultOperator, query, defaultOperator, null, null, null, null, null, null, -1, -1, true);
        request.setAttribute("REP_SEARCH_RESULTS", representatives);
    }

    private void doSearchInstitutions(String query, HttpServletRequest request){
        Collection<Institution> institutions = lookupInstitutionFacade().search(
                    query, defaultOperator,
                    null, null,
                    query, defaultOperator,
                    query, defaultOperator,
                    null, null,
                    query, defaultOperator,
                    null, null,
                    null, null,
                    null, -1, -1,
                    true);

            request.setAttribute("INST_SEARCH_RESULTS", institutions);
    }

    private void injectAdvSearchBaseData(HttpServletRequest request) {


        List<InstitutionRegion> regions = lookupInstitutionRegionFacade().findAll();
        List<InstitutionType> types = lookupInstitutionTypeFacade().findAll();
        List<InstitutionFamily> families = lookupInstitutionFamilyFacade().findAll();
        List<City> cities = lookupCityFacade().findAll();
        List<Institution> institutions = lookupInstitutionFacade().findAll();

        request.setAttribute("search_regions", regions);
        request.setAttribute("search_types", types);
        request.setAttribute("search_families", families);
        request.setAttribute("search_cities", cities);
        request.setAttribute("search_institutions", institutions);
        
    }

    private MeetingFacadeLocal lookupMeetingFacade() {
        try {
            Context c = new InitialContext();
            return (MeetingFacadeLocal) c.lookup("java:comp/env/MeetingFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private CityFacadeLocal lookupCityFacade() {
        try {
            Context c = new InitialContext();
            return (CityFacadeLocal) c.lookup("java:comp/env/CityFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private InstitutionFacadeLocal lookupInstitutionFacade() {
        try {
            Context c = new InitialContext();
            return (InstitutionFacadeLocal) c.lookup("java:comp/env/InstitutionFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private RepresentativeFacadeLocal lookupRepresentativeFacade() {
        try {
            Context c = new InitialContext();
            return (RepresentativeFacadeLocal) c.lookup("java:comp/env/RepresentativeFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private InstitutionTypeFacadeLocal lookupInstitutionTypeFacade() {
        try {
            Context c = new InitialContext();
            return (InstitutionTypeFacadeLocal) c.lookup("java:comp/env/InstitutionTypeFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private InstitutionRegionFacadeLocal lookupInstitutionRegionFacade() {
        try {
            Context c = new InitialContext();
            return (InstitutionRegionFacadeLocal) c.lookup("java:comp/env/InstitutionRegionFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private InstitutionFamilyFacadeLocal lookupInstitutionFamilyFacade() {
        try {
            Context c = new InitialContext();
            return (InstitutionFamilyFacadeLocal) c.lookup("java:comp/env/InstitutionFamilyFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
